package com.qy.StudentInfo.handler;

import com.qy.StudentInfo.pojo.SysLog;
import com.qy.StudentInfo.service.SysLogService;
import com.qy.StudentInfo.utils.ExcelUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @program: student-info-master
 * @author: QiYue
 * @create: 20220429 23:09
 **/
@Controller
public class ExcelHandler {
    @Autowired
    private SysLogService sysLogService;

    /**
     * 导出学生选课全部报表
     * @return
     */
    @RequestMapping(value = "/exportAll")
    public void exportAll(HttpServletRequest request, HttpServletResponse response,SysLog sreq) throws Exception {
        //获取数据
        List<SysLog> list = sysLogService.serviceAllList(sreq);
        System.out.println(list.size());
        //excel标题
        String[] title = {"学号", "学生姓名", "性别", "班级","专业","学院"};

        //excel文件名
        String fileName = "选课信息表" + System.currentTimeMillis() + ".xls";

        //sheet名
        String sheetName = "选课信息表";

        String [][] content = new String[list.size()][6];

        for (int i = 0; i < list.size(); i++) {
            content[i] = new String[title.length];
            SysLog obj = list.get(i);
            content[i][0] = obj.getSid();
            content[i][1] = obj.getSname();
            content[i][2] = obj.getSsex();
            content[i][3] = obj.getClassr();
            content[i][4] = obj.getProfession();
            content[i][5] = obj.getCollege();
        }

        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtils.getHSSFWorkbook(sheetName, title, content, null);

        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 导出学生成绩单报表
     */
    @RequestMapping(value = "/scoreAll")
    public void scoreAll(HttpServletRequest request, HttpServletResponse response,SysLog sreq) throws Exception {
        //获取数据
        List<SysLog> list = sysLogService.selectAllScore(sreq);
        System.out.println(list.size());
        //excel标题
        String[] title = {"学号", "学生姓名", "成绩"};

        //excel文件名
        String fileName = "选课学生成绩单" + System.currentTimeMillis() + ".xls";

        //sheet名
        String sheetName = "学生成绩单";

        String [][] content = new String[list.size()][3];

        for (int i = 0; i < list.size(); i++) {
            content[i] = new String[title.length];
            SysLog obj = list.get(i);
            content[i][0] = obj.getSid();
            content[i][1] = obj.getSname();
            content[i][2] = obj.getCredits();
        }

        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtils.getHSSFWorkbook(sheetName, title, content, null);

        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 发送响应流方法
     */
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(), "ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
